﻿using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace YueRu.Common
{
    public class ValidatorHelper
    {
        public class Validator
        {
            /// <summary>
            /// 判断对象是否为Int32类型的数字
            /// </summary>
            /// <param name="expression"></param>
            /// <returns></returns>
            public static bool IsNumeric(object expression)
            {
                return expression != null && IsNumeric(expression.ToString());
            }

            /// <summary>
            /// 判断对象是否为Int32类型的数字
            /// </summary>
            /// <param name="expression"></param>
            /// <returns></returns>
            public static bool IsNumeric(string expression)
            {
                if (expression == null) return false;
                var str = expression;
                if (str.Length <= 0 || str.Length > 11 || !Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
                {
                    return false;
                }
                return (str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1');
            }

            /// <summary>
            /// 验证是否为正整数
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static bool IsInt(string str)
            {
                return Regex.IsMatch(str, @"^[0-9]*$");
            }

            /// <summary>
            /// 是否为Double类型
            /// </summary>
            /// <param name="expression"></param>
            /// <returns></returns>
            public static bool IsDouble(object expression)
            {
                return expression != null && Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
            }

            /// <summary>
            /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
            /// </summary>
            /// <param name="strNumber">要确认的字符串数组</param>
            /// <returns>是则返加true 不是则返回 false</returns>
            public static bool IsNumericArray(string[] strNumber)
            {
                if (strNumber == null)
                {
                    return false;
                }

                return strNumber.Length >= 1 && strNumber.All(IsNumeric);
            }

            /// <summary>
            ///  检测是否符合email格式
            /// </summary>
            /// <param name="strEmail">要判断的email字符串</param>
            /// <returns>判断结果</returns>
            public static bool IsValidEmail(string strEmail)
            {
                return Regex.IsMatch(strEmail, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]");
            }

            public static bool IsValidDoEmail(string strEmail)
            {
                return Regex.IsMatch(strEmail,
                    @"^@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            }

            /// <summary>
            /// 检测是否是正确的Url
            /// </summary>
            /// <param name="strUrl">要验证的Url</param>
            /// <returns>判断结果</returns>
            public static bool IsURL(string strUrl)
            {
                return Regex.IsMatch(strUrl,
                    @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
            }

            /// <summary>
            /// 是否为ip
            /// </summary>
            /// <param name="ip"></param>
            /// <returns></returns>
            public static bool IsIp(string ip)
            {
                return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
            }

            public static bool IsIpSect(string ip)
            {
                return Regex.IsMatch(ip,
                    @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){2}((2[0-4]\d|25[0-5]|[01]?\d\d?|\*)\.)(2[0-4]\d|25[0-5]|[01]?\d\d?|\*)$");
            }

            /// <summary>
            /// 检查颜色值是否为3/6位的合法颜色
            /// </summary>
            /// <param name="color">待检查的颜色</param>
            /// <returns></returns>
            public static bool CheckColorValue(string color)
            {
                if (String.IsNullOrEmpty(color))
                    return false;

                color = color.Trim().Trim('#');

                if (color.Length != 3 && color.Length != 6)
                    return false;

                //不包含0-9  a-f以外的字符
                return !Regex.IsMatch(color, "[^0-9a-f]", RegexOptions.IgnoreCase);
            }

            /// <summary>
            ///  检测是否有Sql危险字符
            /// </summary>
            /// <param name="str">要判断字符串</param>
            /// <returns>判断结果</returns>
            public static bool IsSafeSqlString(string str)
            {
                return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
            }

            /// <summary>
            /// 验证手机号
            /// </summary>
            /// <param name="mobile"></param>
            /// <returns></returns>
            public static bool IsMobile(string mobile)
            {
                if (string.IsNullOrWhiteSpace(mobile))
                {
                    return false;
                }
                return Regex.IsMatch(mobile, @"^(((13[0-9]{1})|(15[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$");
            }


        }
    }
}
